package org.darcy.app.config;

import org.darcy.app.filter.ResponseFilter;
import org.darcy.app.interceptor.FrontTokenInterceptor;
import org.darcy.common.interceptor.SwaggerInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.handler.MappedInterceptor;

/**
 * web配置
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

	@Bean
	public HandlerInterceptor frontTokenInterceptor() {
		return new FrontTokenInterceptor();
	}

	@Bean
	public ResponseFilter responseFilter() {
		return new ResponseFilter();
	}

	@Value("${swagger.basic.username}")
	private String username;
	
	@Value("${swagger.basic.password}")
	private String password;
	
	@Value("${swagger.basic.check}")
	private Boolean check;

	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		// 添加token拦截器
		// addPathPatterns添加需要拦截的命名空间；
		// excludePathPatterns添加排除拦截命名空间

		// 前端用户登录token
		registry.addInterceptor(frontTokenInterceptor()).addPathPatterns("/api/front/**")
				.excludePathPatterns("/api/front/index").excludePathPatterns("/api/front/qrcode/**")
				.excludePathPatterns("/api/front/login/mobile").excludePathPatterns("/api/front/login")
				.excludePathPatterns("/api/front/sendCode").excludePathPatterns("/api/front/wechat/**")
				.excludePathPatterns("/api/front/search/keyword").excludePathPatterns("/api/front/share")
				.excludePathPatterns("/api/front/article/**").excludePathPatterns("/api/front/city/**")
				.excludePathPatterns("/api/front/product/hot").excludePathPatterns("/api/front/product/good")
				.excludePathPatterns("/api/front/products/**").excludePathPatterns("/api/front/reply/**")
				.excludePathPatterns("/api/front/user/service/**").excludePathPatterns("/api/front/logistics")
				.excludePathPatterns("/api/front/groom/list/**").excludePathPatterns("/api/front/config")
				.excludePathPatterns("/api/front/category").excludePathPatterns("/api/front/seckill/*")
				.excludePathPatterns("/api/front/seckill/list/*").excludePathPatterns("/api/front/seckill/detail/*")
				.excludePathPatterns("/api/front/ios/*").excludePathPatterns("/api/front/ios/register/binding/phone")
				.excludePathPatterns("api/front/combination/index").excludePathPatterns("api/front/seckill/index")
				.excludePathPatterns("api/front/bargain/index").excludePathPatterns("api/front/combination/index")
				.excludePathPatterns("api/front/index/product/*").excludePathPatterns("api/front/index/color/config")
				.excludePathPatterns("api/front/image/domain").excludePathPatterns("api/front/product/leaderboard")
				.excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
	}

	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
		registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	@Bean
	public FilterRegistrationBean<ResponseFilter> filterRegister() {
		// 注册过滤器
		FilterRegistrationBean<ResponseFilter> registration = new FilterRegistrationBean<ResponseFilter>(
				responseFilter());
		registration.addUrlPatterns("/*");
		return registration;
	}

	/* 必须在此处配置拦截器,要不然拦不到swagger的静态资源 */
	@Bean
	@ConditionalOnProperty(name = "swagger.basic.enable", havingValue = "true")
	public MappedInterceptor getMappedInterceptor() {
		return new MappedInterceptor(new String[] { "/doc.html", "/webjars/**" },
				new SwaggerInterceptor(username, password, check));
	}
}
